import { App, mall, navigate } from '../../../library/api/index'
var p = 0, c = 0, d = 0
Page({
  data: {
    navMall: 'on',
    navMe: '',
    back: '/pages/mall/index',
    provinceIndex: '',
    cityIndex: '',
    districtIndex: '',
    showAreaPicker: false,
    area: '',
    provinces: [],
    cities: [],
    districts: [],
    tip: 'blank',
    tipData: {},
    address: {},
    name: '',
    tel: '',
    area: '',
    address: '',
    full_address: '',
    hideTextarea: false
  },
  onLoad: function(e) {
    if(e.back) {
      this.setData({
        back: decodeURIComponent(e.back)
      })
    }
    if(this.data.back.indexOf('mall') > -1) {
      this.setData({
        navMall: 'on',
        navMe: ''
      })
    } else if(this.data.back.indexOf('me') > -1) {
      this.setData({
        navMall: '',
        navMe: 'on'
      })
    }

    this.setAreaData(0, 0, 0)
    this.get()
    App.jice.onLoad(e)
  },
  onReady: function() {
    App.jice.track('page', {
      pagepath: '/pages/mall/address/index',
      pageTitle: '待产包-地址',
      pageType: 'view'
    })
    App.logger({
      type: App.G.view,
      channel: App.G.channel,
      target: '待产包-地址'
    })
  },
  onShow: function() {
    App.jice.track('appshow')
  },
  setAreaData: function(p, c, d) {
    if(this.data.provinces.length == 0) {
      this.getArea(0, (data) => {
        this.setData({
          provinces: data
        })
        let code = this.data.provinces[p].code
        this.getArea(code, (data) => {
          this.setData({
            cities: data
          })
          let code = this.data.cities[c].code
          this.getArea(code, (data) => {
            this.setData({
              districts: data
            })
          })
        })
      })
    } else {
      let code = this.data.provinces[p].code
      this.getArea(code, (data) => {
        this.setData({
          cities: data
        })
        let code = this.data.cities[c].code
        this.getArea(code, (data) => {
          this.setData({
            districts: data
          })
        })
      })
    }
  },
  changeArea: function(e) {
    p = e.detail.value[0]
    c = e.detail.value[1]
    d = e.detail.value[2]
    this.setAreaData(p, c, d)
  },
  showAreaPicker: function() {
    this.setData({
      showAreaPicker: true
    })
  },
  areaPickerCancel: function() {
    this.setData({
      showAreaPicker: false
    })
  },
  areaPickerConfirm: function() {
    let area = ''
    if(this.data.provinces[p].name == this.data.cities[c].name) {
      area = this.data.provinces[p].name
    } else {
      area = this.data.provinces[p].name + ',' + this.data.cities[c].name
    }
    if(this.data.cities[c].name != this.data.districts[d].name) {
      area += ',' + this.data.districts[d].name
    }
    this.setData({
      provinceIndex: p,
      cityIndex: c,
      districtIndex: d,
      area: area
    })
    this.areaPickerCancel()
  },
  getArea: function(code, fun) {
    mall.getArea(code, r => {
      fun(r.data, code)
    })
  },
  get: function() {
    mall.getAddress(r => {
      if(r.code == 200) {
        this.setData({
          name: r.data.name,
          tel: r.data.tel,
          area: r.data.area,
          address: r.data.address,
          full_address: r.data.full_address
        })
      } else if(r.code >= 400) {
        this.showErrorTip(r.data)
      }
    }, r => {
      console.log(r)
      this.showErrorTip('获取地址失败，系统错误,请稍候再试！')
    })
  },
  submit: function(e) {
    let form = e.detail.value
    let data = {
      name: form.name,
      tel: form.tel,
      area: form.area,
      address: form.address,
      full_address: form.full_address
    }
    if(data.name == '') {
      this.showErrorTip('保存地址失败，请填写您的姓名！')
      return
    }
    if(data.tel == '') {
      this.showErrorTip('保存地址失败，请填写您的联系电话！')
      return
    }
    if(data.area == '') {
      this.showErrorTip('保存地址失败，请选择您所在的省市！')
      return
    }
    if(data.address == '') {
      this.showErrorTip('保存地址失败，请填写所在街道！')
      return
    }
    mall.saveAddress(data, r => {
      if(r.code == 200) {
        navigate.back(1)
      } else {
        this.showErrorTip(r.data)
      }
    }, r => {
      console.log(r)
      this.showErrorTip('保存地址失败，系统错误,请稍候再试！')
    })
  },
  showErrorTip: function(message) {
    this.setData({
      hideTextarea: true,
      tip: 'error_tip',
      tipData: message
    })
  },
  closeTip: function() {
    this.setData({
      hideTextarea: false,
      tip: 'blank',
      tipData: {}
    })
  }
})